package com.example.pda.ui.query;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.example.pda.R;
import com.example.pda.common.BaseActivity;
import com.example.pda.common.Utils;
import com.example.pda.data.model.PurchaseOrder;
import com.example.pda.databinding.ActivityQueryBinding;
import com.example.pda.view.FormLayoutManger;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

public class QueryActivity extends BaseActivity {
    private ActivityQueryBinding binding;
    private QueryViewModel queryViewModel;
    private String mType;


    public static void start(Context context, String type) {
        Intent intent = new Intent(context, QueryActivity.class);
        intent.putExtra("TYPE", type);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_query);
        binding.setLifecycleOwner(this);
        queryViewModel = new ViewModelProvider(this, new QueryViewModelFactory()).get(QueryViewModel.class);
        registerObserve(queryViewModel);
        mType = getIntent().getStringExtra("TYPE");

//        binding.editQuery.setText("CD230705001");
        binding.btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = binding.editQuery.getText().toString();
                if(TextUtils.isEmpty(string)){
                    return;
                }
                queryViewModel.loadOrderQuery(mType, string);
            }
        });
        queryViewModel.mPurchaseBean.observe(this, new Observer<PurchaseOrder>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onChanged(PurchaseOrder purchaseOrder) {
                if(!Utils.isEmpty(purchaseOrder.getDataList())){
                    adapter.setData(purchaseOrder.getDataList());
                    binding.ddhLayout.setVisibility(View.VISIBLE);
                    binding.setOrder(purchaseOrder);

                    List<String> titles = new ArrayList<>();
                    titles.add("");
                    adapterTitle.setData(titles);
                } else {
                    binding.ddhLayout.setVisibility(View.GONE);
                    initRecyclerView();
                }
            }
        });
        initRecyclerView();
    }

    private FormAdapter2 adapter;
    FormTitleAdapter2 adapterTitle;
    private void initRecyclerView(){
        binding.recyclerTitleView.setLayoutManager(new FormLayoutManger(this));
        adapterTitle = new FormTitleAdapter2();
        binding.recyclerTitleView.setAdapter(adapterTitle);

        FormLayoutManger formLayoutManger = new FormLayoutManger(this, binding.recyclerView, binding.recyclerTitleView);
        binding.recyclerView.setLayoutManager(formLayoutManger);
        adapter = new FormAdapter2();
        binding.recyclerView.setAdapter(adapter);

        binding.recyclerView.setNestedScrollingEnabled(false);

    }
}
